package com.huawei.hwCloudJs.service.a;

import android.content.Context;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import java.io.File;
import java.io.UnsupportedEncodingException;

public class a {
    private static final String a = "AuthCache";
    private static final String b = "jssdkcache";
    private static final long c = 86400000;

    private String a(Context context, String str) {
        if (context == null) {
            return null;
        }
        File cacheDir = context.getCacheDir();
        if (cacheDir == null) {
            return null;
        }
        File file = new File(cacheDir.getPath() + File.separator + str + File.separator);
        if (!(file.exists() || file.mkdir())) {
            Log.e(a, "getCacheDir mkdir error");
        }
        return file.getAbsolutePath() + File.separator;
    }

    private boolean b(com.huawei.hwCloudJs.service.a.a.a aVar) {
        long currentTimeMillis = System.currentTimeMillis() - aVar.d();
        return currentTimeMillis >= 86400000 || currentTimeMillis < 0;
    }

    private void c(String str) {
        String d = d(str);
        if (d != null) {
            File file = new File(d);
            if (file.exists() && !file.delete()) {
                Log.e(a, "removeFile error");
            }
        }
    }

    private String d(String str) {
        if (TextUtils.isEmpty(str)) {
            return str;
        }
        Object e = e(str);
        return !TextUtils.isEmpty(e) ? a(com.huawei.hwCloudJs.b.a.a(), b) + e : str;
    }

    private String e(String str) {
        if (str == null) {
            return null;
        }
        try {
            return Base64.encodeToString(str.getBytes("UTF-8"), 2);
        } catch (UnsupportedEncodingException e) {
            return String.valueOf(str.hashCode());
        }
    }

    public com.huawei.hwCloudJs.service.a.a.a a(String str) {
        com.huawei.hwCloudJs.service.a.a.a b = b(str);
        if (b == null) {
            return null;
        }
        if (!b(b)) {
            return b;
        }
        c(str);
        return null;
    }

    public void a(com.huawei.hwCloudJs.service.a.a.a aVar) {
        String d = d(aVar.a());
        if (d != null) {
            new d(d).a(aVar);
        }
    }

    public com.huawei.hwCloudJs.service.a.a.a b(String str) {
        String d = d(str);
        return (d != null && new File(d).exists()) ? (com.huawei.hwCloudJs.service.a.a.a) new d(d).a() : null;
    }
}
